package com.tg.app.activity.device.settings;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.appbase.custom.base.SettingData;
import com.appbase.custom.constant.VideoQualityConstants;
import com.tange.module.device.feature.DeviceFeatureObjectBox;
import com.tg.app.R;
import com.tg.app.TGDevice;
import com.tg.app.activity.base.DeviceSettingsBaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.SettingAdapter;
import com.tange.module.device.feature.DeviceFeature;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.app.camera.Camera;
import com.tange.module.camera.hub.CameraHub;
import com.tg.app.view.QualityUIHelper;
import com.tg.appcommon.android.TGLog;
import com.tg.appcommon.android.TGToast;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tg.data.media.OnICameraListener;

import java.util.ArrayList;

/**
 * 高清视频设置
 */
public class CloudResolutionActivity extends DeviceSettingsBaseActivity implements OnICameraListener {
    private ArrayList<SettingData> datas = new ArrayList<>();
    private SettingAdapter adapter;

    private Camera camera;
    private DeviceSettingsInfo info;
    private int clickposition;
    private int selectStream;
    private  int oldStream;

    @Override
    protected void onSetupFailed() {
        setPositionByMode(oldStream);
    }


    private void setPositionByMode(int oldMode) {
        if (oldMode == VideoQualityConstants.HIGH_DEFINITION) {
            clickposition = 1;
        } else if (oldMode == VideoQualityConstants.STANDARD_DEFINITION) {
            clickposition = 0;
        }

        setFlag(clickposition);
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        hideActionBar();
        setContentView(R.layout.activity_cloud_resolution);
        initView();
        Intent intent = getIntent();
        info = intent.getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        camera = CameraHub.getInstance().getCamera(info.uuid);
        if (camera != null){
            camera.registerICameraListenerAndConnect(this);
        }

        getStream();
        backClickEvent();
    }

    @Override
    protected void initView() {
        modifyToolBar(R.string.cloud_resolution);
        adapter = new SettingAdapter(datas, this);
        ListView listView = findViewById(R.id.resolution_list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!TGDevice.getInstance().isNetwork(camera)){
                    TGToast.showToast(R.string.txt_network_anomaly);
                }else{
                    clickposition = position;
            //        startSendui();
                    switch (position) {
                        case 0:
                            changeStream(1);//标清
                            break;
                        case 1:
                            changeStream(0);//高清
                            break;
                    }
                    setFlag(clickposition);
                }
            }
        });
    }

    public void changeStream(int resolution) {
        //1920X1088 1280X720  640X320
        oldStream = selectStream;
        selectStream = resolution;
        camera.sendIOCtrl(AVIOCTRLDEFs.IOTYPE_USEREX_IPCAM_SET_CLOUD_VIDEO_QUALITY_REQ, AVIOCTRLDEFs.SMsgAVIoctrlSetStreamCtrlReq.parseContent(0, (byte) resolution));
    }

    private void getStream() {
        int quality = info.cloudResolution;
        SettingData high = new SettingData(getString(R.string.high_definition), 0);
        SettingData normal = new SettingData(getString(R.string.normal_definition), 0);

        if (camera != null) {
            DeviceFeature deviceFeature =  DeviceFeatureObjectBox.queryDeviceFeature(camera.uid);
            if (deviceFeature != null && deviceFeature.getSupportResolutions()!=null) {
                high = new SettingData(QualityUIHelper.getQualityType(deviceFeature, AVIOCTRLDEFs.AVIOCTRL_QUALITY_MAX), 0);
                normal = new SettingData(QualityUIHelper.getQualityType(deviceFeature, AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN), 0);
            }
        }
        datas.add(normal);
        datas.add(high);
        selectStream = quality;
        oldStream = quality;
        setPositionByMode(quality);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void setFlag(int position) {
        hideLoading();
        for (int i = 0; i < datas.size(); i++) {
            if (i == position) {
                datas.get(i).setFlag(1);
            } else {
                datas.get(i).setFlag(0);
            }
        }
        adapter.notifyDataSetChanged();
    }


    @Override
    public void receiveUpdateConnectStates(int state) {
        TGLog.d("state = " + state);
    }

    @Override
    public void receiveIOCtrlData(int type, byte[] data) {
        if (type == AVIOCTRLDEFs.IOTYPE_USEREX_IPCAM_SET_CLOUD_VIDEO_QUALITY_RESP) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    completeSend();
                    info.cloudResolution = selectStream;
                    sendUpdateSettingBroadcast(info);
                }
            });
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (camera != null) {
            camera.unregisterICameraListener(this);
        }
    }
}
